package org.lzy.aibg.controller;

import org.lzy.aibg.pojo.Employee;
import org.lzy.aibg.service.EmployeeService;
import org.lzy.aibg.utils.CommonPage;
import org.lzy.aibg.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {
    // 1.TO-DO: 注入EmployeeService
    @Autowired
    private EmployeeService employeeService;

    // 2.T0-DO: 通过id获取员工
    @RequestMapping("/{id}")
    public CommonResult<Employee> getById(@PathVariable(value = "id") Integer id) {
        Employee employee = employeeService.getById(id);
        return CommonResult.success(employee);
    }
    // 3.T0-DO: 通过id删除员工
    @DeleteMapping("/{id}")
    public CommonResult deleteEmp(@PathVariable(value = "id") Integer id) {
        int count = employeeService.removeById(id);
        return count==1?CommonResult.success(null):CommonResult.failed("失败");
    }

    // 4.T0-DO: 更新员工
    @PutMapping
    public CommonResult updateEmp(@RequestBody Employee employee) {
        int count = employeeService.update(employee);
        return count==1?CommonResult.success(null):CommonResult.failed("失败");
    }

    // 5. TO-DO: 添加员工
    @PostMapping
    public CommonResult addEmp(@RequestBody Employee employee) {
        int count = employeeService.save(employee);
        return count==1?CommonResult.success(null):CommonResult.failed("失败");
    }
    // 6.T0-DO: 获取员工列表
    @RequestMapping("/listAll")
    public CommonResult<List<Employee>> listAll() {
        return CommonResult.success(employeeService.list());
    }

    // 7.T0-DO: 获取所有员工数量
    @RequestMapping("/count")
    public CommonResult getCount() {
        return CommonResult.success(employeeService.count());
    }
    // 8.T0-DO: 员工列表分页
    @RequestMapping(value = "/listByPage")
    public CommonResult<CommonPage<Employee>> getList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "2") Integer pageSize) {
        return null;
    }
}
